<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\Category;
use app\common\model\Says;
use app\common\model\Zans;
use app\common\model\Comment;
use think\Log;

class Say extends Api
{
  protected $noNeedLogin = [];
  protected $noNeedRight = '*';

  public function _initialize()
  {
    parent::_initialize();
    
    $user = $this->auth->getUser();
    $user && $this->uid = $user->id;
  }
  
//查询评论列表
public function getCommonList(){
    $id = $this->request->post('post_id');
    $page = $this->request->post('page');
    $list = Comment::with("userInfo")->where('says_id', $id)->where('parent_id', 0)->paginate(5, false, ['page' => $page]);
    
    foreach ($list as $comment) {
        $comment['children'] = $comment->childComments()->with('toUidInfo')->select();
    }
  
    $this->success('查询评论列表成功', $list);
}
  
//   新增评论
public function addComment(){
    $id = $this->request->post('post_id');
    $content = $this->request->post('content');
    $pid = $this->request->post('pid');
    $to_uid = $this->request->post('to_uid');
    Says::get($id)->comments()->save([
            'content' => $content,
            'comment_user_id' => $this->uid,
            'status' => 1,
            'parent_id' => $pid,
            'to_uid' => $to_uid
        
        ]);
    $this->success('添加评论成功');
}
  
// 点赞
public function addZan(){
    $id = $this->request->post('id');
    $zans = Says::get($id)->zans;
    
    $result = array_map(function($item) {
            return $item->user_id;
        },  $zans);
    $is_zan = in_array($this->uid, $result);
    if($is_zan){
        Zans::where('says_id', $id)->where('user_id', $this->uid)->delete();
        // $this->error('您已经点过赞了', $zans);
    }else{
        Says::get($id)->zans()->save([
            'user_id' => $this->uid
        ]);
    }
    
    
    $this->success('点赞成功', $zans);
}
  
//   查询分类列表
public function getTypes(){
    $list = Category::getCategoryArray();
    $this->success('查询分类列表成功', ['list' => $list]);
}

// 查询动态列表
public function getList(){
    $page = $this->request->post('page');
    $list = Says::with("user,imgs,category,zans")->order('create_time', 'desc')->paginate(10, false, ['page' => $page]);

    foreach ($list as &$value){
        $result = array_map(function($item) {
            return $item->user_id;
        },  $value->zans);
        $value->is_collection = in_array($this->uid, $result);
    }
    $this->success('查询动态列表成功', $list);
}

public function addSay(){
    $content = $this->request->post('content');
    $title = $this->request->post('title');
    $type = $this->request->post('type');
    $post = $this->request->post();
    Log::record('mediamediamedia');
    Log::record($post);
    
    $say = Says::create([
            'content' => $content,
            'title' => $title,
            'user_id' => $this->uid,
            'type' => $type
        ]);
    Log::record($say->id);
    Log::record($post['media']);
    $arr = [];
    foreach ($post['media'] as $value){
        $arr[] = ['path' => $value];
    } 
    Log::record($arr);
    // 添加图片
    Says::get($say->id)->imgs()->saveAll($arr);
    
    // 分类处理
    $tags = $post['tags'];
    Log::record($tags);
    // 如果有新分类就新增
    if($tags['def']){
        Says::get($say->id)->category()->save(['name' => $tags['def']]);
        // Says::get($say->id)->category;
    }
    // 对应已有分类
    if(count($tags['arr'])){
        $ids = [];
        foreach ($tags['arr'] as $value){
            $ids[] = $value['id'];
        }
        Log::record($ids);
        Says::get($say->id)->category()->saveAll($ids);
    }
    
    $this->success('添加动态成功', ['id' => $say->id]);
} 

// 查询动态详情
public function getDetail(){
    $id = $this->request->post('id');
    $say = Says::with("user,imgs,category")->where('id', $id)->select()[0];
    $say->userInfo = $say->user;
    unset($say->user);
    
    $say->media = $say->imgs;
    unset($say->imgs);
    $this->success('查询动态详情成功', $say);
}









  

}